package ct.zxd.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ct.zxd.db.Dao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class GetOrderByCustomer
 */
public class GetOrderByCustomer extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
        response.setContentType("text/xml;character=utf-8");  
        response.setHeader("Cache-Control", "no-cache");

        int display;
        String scondition = "where customer='"+request.getParameter("userId")+"'";
        String string = request.getParameter("status");
        try {
        	display = Integer.parseInt(request.getParameter("display"));
        	//status = Integer.parseInt(request.getParameter("status"));
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return;
		}
        
        //如果是查询已完成，要包括已评论的订单
        if (string.equals("4")) {
        	scondition+= " and (status = '4' OR status = '5')";
        }else if (!string.equals("0")) {
        	scondition+=" and status='"+request.getParameter("status")+"'";
		}
        
        List<Map<String,Object>> listDish = null;
		
        try {
        	
        	Dao dao = new Dao();
        	listDish = dao.getDishList(display, 500000, scondition);
        	
        	try {  
                JSONArray jsonDish = JSONArray.fromObject(listDish);
                JSONObject jb = new JSONObject();    
                jb.put("dish", jsonDish);                
                //json
                response.getWriter().write(jb.toString());  
        
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        	
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
